package sit.mycatalog.ejb.dao;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import sit.mycatalog.jpa.Catnode;
import sit.mycatalog.util.AbstractDao;
import sit.mycatalog.util.ParamBinder;

/**
 * Session Bean implementation class CatnodeDao
 */
@Stateless
@LocalBean
public class CatnodeDao extends AbstractDao<Catnode> {

    /**
     * Default constructor. 
     */
    public CatnodeDao() {
    	super(Catnode.class);
    }
    
    public List<Catnode> findByParent(Integer p_intParentid) throws Exception {
    	List<Catnode> lsCatnode = null;
    	
    	try {
			StringBuilder sql = new StringBuilder();
			sql.append("SELECT c \n");
			sql.append("FROM Catnode c \n");
			sql.append("WHERE c.parentid = :parentid");
			
			ParamBinder pb = new ParamBinder();
			pb.put("parentid", p_intParentid);
			
			lsCatnode = findList(sql, pb);
			
		} catch (Exception e) {
			throw new Exception(e);
		}
    	
    	return lsCatnode;
    }
    
    public Catnode findAllChild(Catnode p_entCatnode) throws Exception {
    	try {
    		List<Catnode> lsCatnode = findByParent(p_entCatnode.getCatid());
    		
    		for (int i = 0; i < lsCatnode.size(); i++) {
				Catnode entCatnode = lsCatnode.get(i);
				findAllChild(entCatnode);
			}
    		
    		p_entCatnode.setChildNode(lsCatnode);
		} catch (Exception e) {
			throw new Exception(e);
		}
    	
    	return p_entCatnode;
    }
    
    public List<Catnode> findAll() throws Exception {
    	List<Catnode> lsCatnode = null;
    	
    	try {
			StringBuilder sql = new StringBuilder();
			sql.append("SELECT c \n");
			sql.append("FROM Catnode c \n");
			sql.append("WHERE c.catid <> :catid");
			
			ParamBinder pb = new ParamBinder();
			pb.put("catid", new Integer(0));
			
			lsCatnode = findList(sql, pb);
			
		} catch (Exception e) {
			throw new Exception(e);
		}
    	
    	return lsCatnode;
    }
    
    public List<Catnode> findAllExcept(Integer p_intCatid) throws Exception {
    	List<Catnode> lsCatnode = null;
    	
    	try {
    		ParamBinder pb = new ParamBinder();
    		
			StringBuilder sql = new StringBuilder();
			sql.append("SELECT c \n");
			sql.append("FROM Catnode c \n");
			
			if(p_intCatid != null) {
				sql.append("WHERE c.catid <> :catid \n");
				pb.put("catid", p_intCatid);
			}
			
			sql.append("ORDER BY c.catid \n");
			
			lsCatnode = findList(sql, pb);
			
		} catch (Exception e) {
			throw new Exception(e);
		}
    	
    	return lsCatnode;
    }
    
    public List<Catnode> findByCattype(String p_strCattype) throws Exception {
    	List<Catnode> lsCatnode = null;
    	
    	try {
			StringBuilder sql = new StringBuilder();
			sql.append("SELECT c \n");
			sql.append("FROM Catnode c \n");
			sql.append("WHERE c.cattype = :cattype");
			
			ParamBinder pb = new ParamBinder();
			pb.put("cattype", p_strCattype);
			
			lsCatnode = findList(sql, pb);
			
		} catch (Exception e) {
			throw new Exception(e);
		}
    	
    	return lsCatnode;
    }
}
